`timescale 1ns / 1ps
/*
 Copyright 2020 Sean Xiao, jxzsxsp@qq.com
 
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
 
 http://www.apache.org/licenses/LICENSE-2.0
 
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

module yue_timer_lsu
(
    input clk,

    input [ 31: 0 ] i_sft_timer_din,
    input i_tmr_sft_we,

//  register group
    input i_tm_ctrl_cs,
    output reg [ 31: 0 ] o_tm_ctrl,

    input i_sft_cs,
    output reg [ 31: 0 ] o_sft_int_v,

    input i_tcs0,
    output reg [ 31: 0 ] o_timer_l,

    input i_tcs1,
    output reg [ 31: 0 ] o_timer_h,

    input i_tcmp_cs0,
    output reg [ 31: 0 ] o_tcmp_l,

    input i_tcmp_cs1,
    output reg [ 31: 0 ] o_tcmp_h,

    output reg [ 1: 0 ] o_timer_valid,

    input rst_n
);

always@( posedge clk or negedge rst_n )
if ( !rst_n )
begin
    o_tm_ctrl <= 0;
    o_sft_int_v <= 0;
    o_timer_l <= 0;
    o_timer_h <= 0;
    o_tcmp_l <= 0;
    o_tcmp_h <= 16'h8000;
    o_timer_valid <= 0;
end
else
begin
    o_timer_valid <= 0;

    if ( i_tmr_sft_we )
    begin
        if ( i_sft_cs )
            o_sft_int_v <= { 31'b0, i_sft_timer_din[ 0 ] };

        if ( i_tcs0 )
        begin
            o_timer_l <= i_sft_timer_din;
            o_timer_valid[ 0 ] <= 1'b1;
        end

        if ( i_tcs1 )
        begin
            o_timer_h <= i_sft_timer_din;
            o_timer_valid[ 1 ] <= 1'b1;
        end

        if ( i_tcmp_cs0 )
            o_tcmp_l <= i_sft_timer_din;

        if ( i_tcmp_cs1 )
            o_tcmp_h <= i_sft_timer_din;

        if ( i_tm_ctrl_cs )
            o_tm_ctrl <= i_sft_timer_din;
    end
end


endmodule
